﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;


namespace QuanLyBanHang.SystemForms
{
    /// <summary>
    /// Form thong tin nguoi dung
    /// </summary>
    /// <author>Viet sang</author>
    /// <date>26/10/2011</date>
    /// 
    public partial class frmInfoOfUser : STLGROUP.Layers.Presentations.FormBase.frmBase
    {
        #region Variables

        private ACL_TaiKhoan _user;       
        private string _userNameFirst;
        private string _reminderPassFirst;

        #endregion

        #region Constructors

        public frmInfoOfUser()
        {
            InitializeComponent();

            this.Load+=new EventHandler(frmInfoOfUser_Load);

            pnlTaiKhoan.Enabled = false;
            pnlMatKhau.Visible = false;
            tsbHuy.Visible = false;
        }

        #endregion

        #region Events

        private void frmInfoOfUser_Load(object sender, EventArgs e)
        {
            if (User != null && User.IDNhanVien.HasValue )
            {
                DT_NhanVien nhanVien = Cache.Context.DT_NhanVien.Where(s => s.IDNhanVien == User.IDNhanVien.Value).FirstOrDefault();
                if (nhanVien != null)
                {
                    lblAddress.Text = nhanVien.DiaChi;
                    lblDateOfBirth.Text = nhanVien.NgaySinhText;
                    lblEmployeeID.Text = nhanVien.MaNhanVien;
                    lblEmployeeName.Text = nhanVien.TenDayDu;
                    lblPhoneNumber.Text = nhanVien.SoDienThoai;

                    txtUserName.Text = User.TenDangNhap.Trim();
                    _userNameFirst = User.TenDangNhap.Trim();

                    txtPassword.Text = User.MatKhau;
                    txtReminderPass.Text = User.GoiYMatKhau.Trim();
                    _reminderPassFirst = User.GoiYMatKhau.Trim();
                }
                
            }
        }

        private void tsbThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbSua_Click(object sender, EventArgs e)
        {
            pnlTaiKhoan.Enabled = true;
            tsbHuy.Visible = true;
            txtUserName.Focus();
        }

        private void tsbLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (pnlTaiKhoan.Enabled == true)
                {
                    User.TenDangNhap = txtUserName.Text.Trim();
                    User.GoiYMatKhau = txtReminderPass.Text.Trim();

                    pnlTaiKhoan.Enabled = false;
                    tsbHuy.Visible = false;
                }
                if (pnlMatKhau.Visible == true)
                {
                    bool passOlder = UICommon.Decoding(txtOlderPass.Text.Trim(), User.MatKhau.Trim());
                    if (passOlder == true && !string.IsNullOrEmpty(txtNewPass.Text))
                    {
                        if (txtNewPass.Text.CompareTo(txtRePass.Text) == 0)
                        {
                            User.MatKhau = UICommon.Encryption(txtNewPass.Text);
                            tsbHuy.Visible = false;

                            txtOlderPass.Text = string.Empty;
                            txtNewPass.Text = string.Empty;
                            txtRePass.Text = string.Empty;

                            pnlMatKhau.Visible = false;
                        }
                        else
                        {
                            MessageBox.Show("Mật khẩu mới chưa chính xác vui lòng nhập lại");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Mật khẩu cũ chưa đúng vui lòng kiểm tra lại");
                    }
                }

                Cache.Context.SaveChanges();
            }
            catch (Exception ex)
            {
                UICommon.ShowMessgeString(ex.Message, "Lỗi cập nhật");
            }
        }

        private void tsbDoiMatKhau_Click(object sender, EventArgs e)
        {
            pnlMatKhau.Visible = true;
            tsbHuy.Visible = true;
            txtOlderPass.Focus();
        }

        private void tsbHuy_Click(object sender, EventArgs e)
        {
            txtUserName.Text = _userNameFirst;
            txtReminderPass.Text = _reminderPassFirst;
            
            pnlTaiKhoan.Enabled = false;
            pnlMatKhau.Visible = false;
            tsbHuy.Visible = false;
        }

        #endregion

        #region Properties

        public ACL_TaiKhoan User
        {
            get { return _user; }
            set { _user = value; }
        }

        #endregion

    }
         
}
